<?php
namespace Common\Behaviors;

use Common\Model\ConfigModel;
use Think\Behavior;
use wechat\WeChat;

class AppBeginBehavior extends Behavior {

    /**
     * 执行行为 run方法是Behavior唯一的接口
     * @access public
     * @param mixed $params 行为参数
     * @return void
     */
    public function run(&$params) {
        $this->loadConfig();
        $this->wechatConfig();
    }


    // 加载配置项
    protected function loadConfig() {
        $data = (new ConfigModel())->find(1);
        $data['system'] = json_decode($data['system'], true);
        $data['wechat'] = json_decode($data['wechat'], true);
        v('config.system',$data['system']);
        v('config.wechat',$data['wechat']);
        define('MODULE',I('get.mo','','ucfirst'));
        return true;
    }

    // 加载微信配置
    private function wechatConfig(){
        $config = [
            "token"          => v('config.wechat.token'),
            "appid"          => v('config.wechat.appid'),
            "appsecret"      => v('config.wechat.appsecret'),
        ];
        new WeChat($config);
    }


}